<?php defined('BASEPATH') or exit('No direct script access allowed');

/**
 * 线路模块--供应商
 *
 * Class Line_Line_supplier_model
 */
class Line_Site_view_model extends CI_Model
{
	/**
	 *
	 * 更新数据
	 * @param array $data 数据数组
	 * @param array $where 更新条件
	 */
	function edit_row($data,$where){
		$table = $this->get_table_name();
		if(!empty($where))$this->db->where($where, NULL, FALSE);
		return $this->db->update($table,$data);
	}

	/**
	 *
	 * 添加数据
	 * @param $data
	 */
	function add_row($data){
		$table = $this->get_table_name();
		if(is_array($data)){
			if($this->db->insert($table,$data)){
				return $this->db->insert_id();
			}else{
				return false;
			}
		}
	}

	/**
	 *
	 * 获取单条记录
	 * @param array $where 查询条件
	 */
	function get_row($where=array(),$field='*'){
		$table = $this->get_table_name();
		if(!empty($field))$this->db->select($field);
		$query = $this->db->get_where($table,$where,1);
		$row = $query->row_array();
		return $row;
	}

	/**
	 *
	 * 删除数据
	 * @param array $where 删除条件
	 */
	function remove_row($where=array()){
		$table = $this->get_table_name();
		if(!empty($where))$this->db->where($where, NULL, FALSE);
		return $this->db->delete($table);
	}

	/**
	 *
	 * 获取列表
	 * @param $where
	 */
     function get_list($page_size=20,$now_page=1,$where){
        $table = $this->get_table_name();
        $this->db->select('*');
        if(!empty($where))$this->db->where($where, NULL, FALSE);
        $query = $this->db->get($table,$page_size,($now_page-1)*$page_size);
		$data = $query->result_array();
        return $data;
	}
	
	/**
	 * 获取总数
	 *
	 */
	function get_count($where){
		$table = $this->get_table_name();
		if(!empty($where))$this->db->where($where, NULL, FALSE);
		$this->db->select('id');
		$this->db->from($table);
		return $this->db->count_all_results();
	}
	/**
	 *
	 * 获取数据表名称
	 */
	function get_table_name(){
		$table = $this->db->dbprefix("site_view");
		return $table;
	}

	function update_data($type='views'){
		$table = $this->get_table_name();
		$cur_date = date('Y-m-d');
		$sql = "update $table set {$type}={$type}+1 where sid=".$GLOBALS['sid']." and add_date='".$cur_date."' ";
		$this->db->query($sql);
		if(!$this->db->affected_rows()){
			return $this->add_row(array("{$type}"=>1,'sid'=>$GLOBALS['sid'],'add_date'=>$cur_date));
		}
	}

	function fetch_count_by_date($start,$end,$type='views'){
		$table = $this->get_table_name();
		$sql = "SELECT {$type} AS c FROM $table WHERE sid=".SITEID." AND add_date='$start'";
		$query = $this->db->query($sql);

		$data = $query->row_array();
		if(empty($data))return 0;
		return $data['c'];
	}

}
